package ttp.utilities;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

public class DatabaseUtil {
	//1. 싱글톤 패턴
	private static DatabaseUtil instance;

	private DatabaseUtil(){
		dataSource = new BasicDataSource();
		//DB연결 관련 설정
		dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");//DriverClass설정	
		dataSource.setUrl("jdbc:oracle:thin:@127.0.0.1:1521:XE");//Url설정
		dataSource.setUsername("scott");
		dataSource.setPassword("tiger");//계정설정

	}
	public static DatabaseUtil getInstance(){
		//처음 요청이 들어올 때 객체 생성
		if(instance==null){
			instance = new DatabaseUtil();
		}
		return instance;
	}
	private BasicDataSource dataSource;
	public Connection getConnection() throws Exception {
		//2. Connection을 대여하는 메소드(BasicDataSource로부터)
		return dataSource.getConnection();
	}
	//3. JDBC연결을 닫는 메소드
	public void close(Connection conn, Statement stmt) throws SQLException{
		if(stmt!=null) stmt.close();
		if(conn!=null) conn.close();//반납
	}
	public void close(Connection conn, Statement stmt, ResultSet rset) throws SQLException{
		if(rset!=null) rset.close();
		if(stmt!=null) stmt.close();
		if(conn!=null) conn.close();
	}
}

